package com.zb.jsp.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.zb.jsp.vo.UserVo;

/**
 * 数据库操作
 */
public class UserInfoDao {
	private static String URL = "jdbc:mysql://localhost:3306/ggyx?useUnicode=true&characterEncoding=utf-8";
	private static String USERNAME = "root";
	private static String PWD = "sa";

	/**
	 * 根据用户名查询数据库信息
	 * 
	 * @param userName
	 *            要查找的用户名
	 * @throws SQLException
	 *             数据库链接异常
	 */
	public UserVo findUserInfoByUserName(String email ,int flg) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		UserVo uservo = new UserVo();

		// 注册mysql驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");

			// 获取连接
			conn = DriverManager.getConnection(URL, USERNAME, PWD);
			// 创建statment对象
			stmt = conn.createStatement();
			if(flg==0){
				// 按用户名查询 sql语句管理员用户
				String sql = "select * from tbl_manager where email ='" + email + "'";
				// 将查询结果返回数据集
				rs = stmt.executeQuery(sql);
				
				if (rs.next()) {
					uservo.setUserName(email);
					uservo.setPasswd(rs.getString("passwd"));
					uservo.setUserNameCn(rs.getString("user_cn"));
				}
			}else{
				// 按用户名查询 sql语句 select * from tbl_register_user where email = 'daf';
				String sql = "select * from tbl_register_user where email ='" + email + "'";
				// 将查询结果返回数据集
				rs = stmt.executeQuery(sql);

				if (rs.next()) {
					uservo.setUserName(email);
					uservo.setPasswd(rs.getString("passwd"));
					uservo.setUserNameCn(rs.getString("name_cn"));
				}
			}
			

		} catch (ClassNotFoundException e) {
			// 捕获注册驱动异常
			e.printStackTrace();
		} finally {
			// 倒序关闭连接
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		}
		return uservo;
	}
}
